package com.util;

import java.io.Serializable;
import javax.servlet.http.HttpSession;

/**
 * @author yangguanzan
 * @date 9/20/21
 * @description
 */
public class SessionUtils {

    private SessionUtils() {
        //not do
    }
    private static final String USER="USER";

    public static void setUser(HttpSession session, Serializable user) {
        session.setAttribute(USER, user);
    }

    public static void removeUser(HttpSession session) {
        if(null!=session){
            session.removeAttribute(USER);
        }
    }

    public static void setVerifyCode(HttpSession session,String verifyCode) {
        //删除以前的
        session.removeAttribute(VerifyCodeUtils.VERIFY_CODE_FLAG);
        session.setAttribute(VerifyCodeUtils.VERIFY_CODE_FLAG, verifyCode.toLowerCase());
    }

    public static Object getVerifyCode(HttpSession session) {
        if(null==session){
            return "";
        }
        return session.getAttribute(VerifyCodeUtils.VERIFY_CODE_FLAG);
    }

    public static boolean hasUserSession(HttpSession session) {
        if(null==session){
            return false;
        }
        return null!=session.getAttribute(USER);
    }

    public static Object getUser(HttpSession session) {
        if(null==session){
            return null;
        }
        return session.getAttribute(USER);

    }
}
